Find in files within Vim
Basic Use A useful feature in many text editors is the ability to search for regular expressions in multiple files. Vim has this feature, but it is a little hard to find. Vim provides these functions for doing this: *grep *lgrep *vimgrep *lvimgrep All of these commands can be used to search for a regular expression in whatever files you specify. "grep" and "lgrep" use an external application to perform the search, and are great if you are running Vim on a system with a good file searching utility, if you are accustomed to using an external application for your searches, or if your search is not a complicated one. "vimgrep" and "lvimgrep" are part of Vim, and therefore are good for using on any system, especially if you want to use Vim-style regular expressions in your search. These commands all fill a list with the results of their search. "grep" and "vimgrep" fill the "quickfix list", which can be opened with :cw or :copen, and is a list shared between ALL windows. "lgrep" and "lvimgrep" fill the "location list," which is local to the current window, and can be opened with :lw or :lopen. Use of grep and lgrep depend on the external application they point to, but use of vimgrep and lvimgrep is as follows: :vimgrep! /{pattern}/gj {file} ... The 'g' option specifies that all matches for a search will be returned instead of just one per line, and the 'j' option specifies that Vim will not jump to the first match automatically. For example, to search for the words "house" or "home" in all .txt files in the current directory, use: :lvim /\<\(house\|home\)\>/gj *.txt :lw Tips and Tricks Recursive Search You can use ** in the file pattern to search recursively. For example, to search for all lines containing "dostuff()" in all .c files in the parent directory and all its subdirectories, use: :vimgrep /dostuff()/j ../**/*.c Path Separators In the Windows version of Vim, backslash ('\') characters can be used as path separators, but using ('/') allows file paths in these searches to work in Vim under any operating system. This is especially useful if your vimrc file is used on multiple platforms, and you want to write mappings for common searches as below. Mappings You can use a mapping, command, or abbreviation for common searches, like a search for a word under the cursor in all files of the same type in the current directory. This can be especially useful if you have it automatically open the results list as well. This mapping uses to get the word under the cursor, and searches for it in the current directory and all subdirectories, opening the quickfix window when done: map :execute "vimgrep /" . expand("") . "/j **" cw This command based on VimTip483 uses to get the word under the cursor, but also uses % to limit the search to the current file only: command GREP :execute 'vimgrep /'.expand().'/gj '.expand('%') | copen Finally, this cabbrev uses CTRL-R CTRL-W to get the word under the cursor, limiting the search to files of the same type using expand("%:e"), and moving the cursor on the command-line to be just before the file list using , so that you can easily specify a different search location. This method is nice because you can either type :lvim to perform the default search, or :lvim to enter the default search into the command line, but leave it there for editing. cabbrev lvim \ lvim /\\>/gj \ *=(expand("%:e") "" ? "" : ".".expand("%:e")) \ lw \ Note: using a cabbrev and a mapping together may not work very well, because the mapping will trigger the cabbrev. Also, be careful not to use common text in your cabbrev, such as making one for the "vim" command, because then whenever you try to edit a .vim file for example, the cabbrev will be expanded when you press Enter. References * * * * * * * * Comments '''TODO:' VimTip1214 and VimTip483 should be merged here. Two of the examples in "tips and tricks" are from these tips. Related tips *483 Search using quickfix to list occurrences *1214 Recursive vimgrep *1234 Find files in subdirectories *1392 Shell script to use grep with gvim *1008 Toggle to open or close the quickfix window Probably should add a "See also" to 483 from here, and vice versa, and keep 483 (one day maybe merge). Should merge 1214. A quick look makes me think 1234 and 1392 are misguided and should be deleted. Maybe flag them deprecated and delete later. We need a tip on the quickfix window. I map Alt-F11 to :copen, F11 to :cnext and Shift-F11 to :cprev. It's sensational, but I don't recall a tip with that simple info. Tip 1008 (above) is the only relevant tip, and it seems too weird to me. --JohnBeckett 08:24, 10 January 2008 (UTC) ---- Category:Searching